package com.lill.stock.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lill.org.model.CompanyBranchModel;
import com.lill.org.service.ICompanyBranchService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lill.common.component.util.IdUtil;
import com.lill.stock.dao.IStockInnerDao;
import com.lill.stock.model.StockInnerDetailModel;
import com.lill.stock.model.StockInnerModel;
import com.lill.stock.service.IStockInnerService;

/**
 * 入库单Service实现类
 *
 * @author lill
 * @date 2021-09-07 19:53:18
 */
@Service("com.lill.stock.service.impl.StockInnerServiceImpl")
public class StockInnerServiceImpl implements IStockInnerService {
	
    @Autowired
    private IStockInnerDao stockInnerDao;

    @Autowired
    private ICompanyBranchService companyBranchService;

    public StockInnerModel queryObject(String id) {
        return stockInnerDao.queryObject(id);
    }

    public List<StockInnerModel> queryList(Map<String, Object> map) {
        List<StockInnerModel> list = stockInnerDao.queryList(map);
        Map<String, String> mapInfo=new HashMap<String, String>();
        if(CollectionUtils.isNotEmpty(list)){
            for (StockInnerModel stockInnerModel : list) {
                if(mapInfo.containsKey(stockInnerModel.getBranchId())){
                    stockInnerModel.setBranchName(mapInfo.get(stockInnerModel.getBranchId()));
                }else{
                    CompanyBranchModel companyBranch=companyBranchService.queryObject(stockInnerModel.getBranchId());
                    if(null!=companyBranch){
                        stockInnerModel.setBranchName(companyBranch.getName());
                        mapInfo.put(stockInnerModel.getBranchId(), companyBranch.getName());
                    }else{
                        mapInfo.put(stockInnerModel.getBranchId(), "");
                    }
                }
            }
        }
        return list;
    }

    public int queryTotal(Map<String, Object> map) {
        return stockInnerDao.queryTotal(map);
    }

    public int save(StockInnerModel stockInner) {
        stockInner.setId(IdUtil.createIdbyUUID());
        for (StockInnerDetailModel innerDetail : stockInner.getProductList()) {
        	innerDetail.setId(IdUtil.createIdbyUUID());
		}
        stockInnerDao.saveBatchDetail(stockInner);
        stockInnerDao.saveBatchReal(stockInner);
        return stockInnerDao.save(stockInner);
    }

	public int deleteLogicByMap(Map<String, Object> map) {
		return stockInnerDao.deleteLogicByMap(map);
	}
}
